********************************************************************************
** 	TITLE:		de2017_gles_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ZA6802_en_v3-0-1_en                                           **
**	DATE:		October 2022 					                              **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "de2017_gles_contact_networks", replace

* Open the dataset

use "ZA6802_en_v3-0-1_en.dta", clear

* Generate ID variable

gen id = vlfdn

drop if id == -94 // Drop if "not in sampling frame"


*****************************
** DISTRICT-LEVEL OUTCOMES **
*****************************

* Note: 1 = CDU/CSU, 4 = SPD, 5 = FDP, 6 = Green, 7 = Die Linke, 322 = AfD

gen district_outcome = .

replace district_outcome = 1 if wahlkreis == 1
replace district_outcome = 1 if wahlkreis == 2
replace district_outcome = 1 if wahlkreis == 3
replace district_outcome = 1 if wahlkreis == 4
replace district_outcome = 4 if wahlkreis == 5
replace district_outcome = 1 if wahlkreis == 6
replace district_outcome = 1 if wahlkreis == 7
replace district_outcome = 1 if wahlkreis == 8
replace district_outcome = 1 if wahlkreis == 9
replace district_outcome = 1 if wahlkreis == 10
replace district_outcome = 1 if wahlkreis == 11
replace district_outcome = 1 if wahlkreis == 12
replace district_outcome = 1 if wahlkreis == 13
replace district_outcome = 1 if wahlkreis == 14
replace district_outcome = 1 if wahlkreis == 15
replace district_outcome = 1 if wahlkreis == 16
replace district_outcome = 1 if wahlkreis == 17
replace district_outcome = 4 if wahlkreis == 18
replace district_outcome = 4 if wahlkreis == 19
replace district_outcome = 4 if wahlkreis == 20
replace district_outcome = 1 if wahlkreis == 21
replace district_outcome = 4 if wahlkreis == 22
replace district_outcome = 4 if wahlkreis == 23
replace district_outcome = 4 if wahlkreis == 24
replace district_outcome = 1 if wahlkreis == 25
replace district_outcome = 4 if wahlkreis == 26
replace district_outcome = 4 if wahlkreis == 27
replace district_outcome = 1 if wahlkreis == 28
replace district_outcome = 1 if wahlkreis == 29
replace district_outcome = 1 if wahlkreis == 30
replace district_outcome = 1 if wahlkreis == 31
replace district_outcome = 1 if wahlkreis == 32
replace district_outcome = 1 if wahlkreis == 33
replace district_outcome = 1 if wahlkreis == 34
replace district_outcome = 4 if wahlkreis == 35
replace district_outcome = 1 if wahlkreis == 36
replace district_outcome = 1 if wahlkreis == 37
replace district_outcome = 1 if wahlkreis == 38
replace district_outcome = 1 if wahlkreis == 39
replace district_outcome = 1 if wahlkreis == 40
replace district_outcome = 4 if wahlkreis == 41
replace district_outcome = 4 if wahlkreis == 42
replace district_outcome = 1 if wahlkreis == 43
replace district_outcome = 1 if wahlkreis == 44
replace district_outcome = 4 if wahlkreis == 45
replace district_outcome = 4 if wahlkreis == 46
replace district_outcome = 4 if wahlkreis == 47
replace district_outcome = 4 if wahlkreis == 48
replace district_outcome = 4 if wahlkreis == 49
replace district_outcome = 4 if wahlkreis == 50
replace district_outcome = 4 if wahlkreis == 51
replace district_outcome = 1 if wahlkreis == 52
replace district_outcome = 4 if wahlkreis == 53
replace district_outcome = 4 if wahlkreis == 54
replace district_outcome = 4 if wahlkreis == 55
replace district_outcome = 1 if wahlkreis == 56
replace district_outcome = 1 if wahlkreis == 57
replace district_outcome = 1 if wahlkreis == 58
replace district_outcome = 1 if wahlkreis == 59
replace district_outcome = 1 if wahlkreis == 60
replace district_outcome = 4 if wahlkreis == 61
replace district_outcome = 1 if wahlkreis == 62
replace district_outcome = 1 if wahlkreis == 63
replace district_outcome = 1 if wahlkreis == 64
replace district_outcome = 1 if wahlkreis == 65
replace district_outcome = 1 if wahlkreis == 66
replace district_outcome = 1 if wahlkreis == 67
replace district_outcome = 1 if wahlkreis == 68
replace district_outcome = 1 if wahlkreis == 69
replace district_outcome = 1 if wahlkreis == 70
replace district_outcome = 1 if wahlkreis == 71
replace district_outcome = 1 if wahlkreis == 72
replace district_outcome = 1 if wahlkreis == 73
replace district_outcome = 1 if wahlkreis == 74
replace district_outcome = 4 if wahlkreis == 75
replace district_outcome = 7 if wahlkreis == 76
replace district_outcome = 1 if wahlkreis == 77
replace district_outcome = 4 if wahlkreis == 78
replace district_outcome = 1 if wahlkreis == 79
replace district_outcome = 1 if wahlkreis == 80
replace district_outcome = 1 if wahlkreis == 81
replace district_outcome = 4 if wahlkreis == 82
replace district_outcome = 6 if wahlkreis == 83
replace district_outcome = 7 if wahlkreis == 84
replace district_outcome = 7 if wahlkreis == 85
replace district_outcome = 7 if wahlkreis == 86
replace district_outcome = 1 if wahlkreis == 87
replace district_outcome = 4 if wahlkreis == 88
replace district_outcome = 1 if wahlkreis == 89
replace district_outcome = 1 if wahlkreis == 90
replace district_outcome = 1 if wahlkreis == 91
replace district_outcome = 1 if wahlkreis == 92
replace district_outcome = 1 if wahlkreis == 93
replace district_outcome = 1 if wahlkreis == 94
replace district_outcome = 4 if wahlkreis == 95
replace district_outcome = 4 if wahlkreis == 96
replace district_outcome = 1 if wahlkreis == 97
replace district_outcome = 1 if wahlkreis == 98
replace district_outcome = 1 if wahlkreis == 99
replace district_outcome = 1 if wahlkreis == 100
replace district_outcome = 4 if wahlkreis == 101
replace district_outcome = 4 if wahlkreis == 102
replace district_outcome = 1 if wahlkreis == 103
replace district_outcome = 1 if wahlkreis == 104
replace district_outcome = 1 if wahlkreis == 105
replace district_outcome = 1 if wahlkreis == 106
replace district_outcome = 1 if wahlkreis == 107
replace district_outcome = 1 if wahlkreis == 108
replace district_outcome = 1 if wahlkreis == 109
replace district_outcome = 1 if wahlkreis == 110
replace district_outcome = 1 if wahlkreis == 111
replace district_outcome = 1 if wahlkreis == 112
replace district_outcome = 1 if wahlkreis == 113
replace district_outcome = 1 if wahlkreis == 114
replace district_outcome = 4 if wahlkreis == 115
replace district_outcome = 4 if wahlkreis == 116
replace district_outcome = 4 if wahlkreis == 117
replace district_outcome = 4 if wahlkreis == 118
replace district_outcome = 4 if wahlkreis == 119
replace district_outcome = 1 if wahlkreis == 120
replace district_outcome = 4 if wahlkreis == 121
replace district_outcome = 4 if wahlkreis == 122
replace district_outcome = 4 if wahlkreis == 123
replace district_outcome = 1 if wahlkreis == 124
replace district_outcome = 4 if wahlkreis == 125
replace district_outcome = 1 if wahlkreis == 126
replace district_outcome = 1 if wahlkreis == 127
replace district_outcome = 1 if wahlkreis == 128
replace district_outcome = 1 if wahlkreis == 129
replace district_outcome = 1 if wahlkreis == 130
replace district_outcome = 1 if wahlkreis == 131
replace district_outcome = 4 if wahlkreis == 132
replace district_outcome = 4 if wahlkreis == 133
replace district_outcome = 4 if wahlkreis == 134
replace district_outcome = 1 if wahlkreis == 135
replace district_outcome = 1 if wahlkreis == 136
replace district_outcome = 1 if wahlkreis == 137
replace district_outcome = 4 if wahlkreis == 138
replace district_outcome = 4 if wahlkreis == 139
replace district_outcome = 4 if wahlkreis == 140
replace district_outcome = 4 if wahlkreis == 141
replace district_outcome = 4 if wahlkreis == 142
replace district_outcome = 4 if wahlkreis == 143
replace district_outcome = 4 if wahlkreis == 144
replace district_outcome = 4 if wahlkreis == 145
replace district_outcome = 1 if wahlkreis == 146
replace district_outcome = 1 if wahlkreis == 147
replace district_outcome = 1 if wahlkreis == 148
replace district_outcome = 1 if wahlkreis == 149
replace district_outcome = 4 if wahlkreis == 150
replace district_outcome = 1 if wahlkreis == 151
replace district_outcome = 1 if wahlkreis == 152
replace district_outcome = 7 if wahlkreis == 153
replace district_outcome = 1 if wahlkreis == 154
replace district_outcome = 1 if wahlkreis == 155
replace district_outcome = 322 if wahlkreis == 156
replace district_outcome = 322 if wahlkreis == 157
replace district_outcome = 322 if wahlkreis == 158
replace district_outcome = 1 if wahlkreis == 159
replace district_outcome = 1 if wahlkreis == 160
replace district_outcome = 1 if wahlkreis == 161
replace district_outcome = 1 if wahlkreis == 162
replace district_outcome = 1 if wahlkreis == 163
replace district_outcome = 1 if wahlkreis == 164
replace district_outcome = 1 if wahlkreis == 165
replace district_outcome = 1 if wahlkreis == 166
replace district_outcome = 4 if wahlkreis == 167
replace district_outcome = 4 if wahlkreis == 168
replace district_outcome = 4 if wahlkreis == 169
replace district_outcome = 4 if wahlkreis == 170
replace district_outcome = 4 if wahlkreis == 171
replace district_outcome = 1 if wahlkreis == 172
replace district_outcome = 1 if wahlkreis == 173
replace district_outcome = 1 if wahlkreis == 174
replace district_outcome = 1 if wahlkreis == 175
replace district_outcome = 1 if wahlkreis == 176
replace district_outcome = 1 if wahlkreis == 177
replace district_outcome = 1 if wahlkreis == 178
replace district_outcome = 1 if wahlkreis == 179
replace district_outcome = 1 if wahlkreis == 180
replace district_outcome = 1 if wahlkreis == 181
replace district_outcome = 1 if wahlkreis == 182
replace district_outcome = 1 if wahlkreis == 183
replace district_outcome = 1 if wahlkreis == 184
replace district_outcome = 1 if wahlkreis == 185
replace district_outcome = 1 if wahlkreis == 186
replace district_outcome = 1 if wahlkreis == 187
replace district_outcome = 1 if wahlkreis == 188
replace district_outcome = 1 if wahlkreis == 189
replace district_outcome = 1 if wahlkreis == 190
replace district_outcome = 1 if wahlkreis == 191
replace district_outcome = 1 if wahlkreis == 192
replace district_outcome = 1 if wahlkreis == 193
replace district_outcome = 1 if wahlkreis == 194
replace district_outcome = 1 if wahlkreis == 195
replace district_outcome = 1 if wahlkreis == 196
replace district_outcome = 1 if wahlkreis == 197
replace district_outcome = 1 if wahlkreis == 198
replace district_outcome = 1 if wahlkreis == 199
replace district_outcome = 1 if wahlkreis == 200
replace district_outcome = 1 if wahlkreis == 201
replace district_outcome = 1 if wahlkreis == 202
replace district_outcome = 1 if wahlkreis == 203
replace district_outcome = 1 if wahlkreis == 204
replace district_outcome = 1 if wahlkreis == 205
replace district_outcome = 1 if wahlkreis == 206
replace district_outcome = 1 if wahlkreis == 207
replace district_outcome = 1 if wahlkreis == 208
replace district_outcome = 4 if wahlkreis == 209
replace district_outcome = 1 if wahlkreis == 210
replace district_outcome = 1 if wahlkreis == 211
replace district_outcome = 1 if wahlkreis == 212
replace district_outcome = 1 if wahlkreis == 213
replace district_outcome = 1 if wahlkreis == 214
replace district_outcome = 1 if wahlkreis == 215
replace district_outcome = 1 if wahlkreis == 216
replace district_outcome = 1 if wahlkreis == 217
replace district_outcome = 1 if wahlkreis == 218
replace district_outcome = 1 if wahlkreis == 219
replace district_outcome = 1 if wahlkreis == 220
replace district_outcome = 1 if wahlkreis == 221
replace district_outcome = 1 if wahlkreis == 222
replace district_outcome = 1 if wahlkreis == 223
replace district_outcome = 1 if wahlkreis == 224
replace district_outcome = 1 if wahlkreis == 225
replace district_outcome = 1 if wahlkreis == 226
replace district_outcome = 1 if wahlkreis == 227
replace district_outcome = 1 if wahlkreis == 228
replace district_outcome = 1 if wahlkreis == 229
replace district_outcome = 1 if wahlkreis == 230
replace district_outcome = 1 if wahlkreis == 231
replace district_outcome = 1 if wahlkreis == 232
replace district_outcome = 1 if wahlkreis == 233
replace district_outcome = 1 if wahlkreis == 234
replace district_outcome = 1 if wahlkreis == 235
replace district_outcome = 1 if wahlkreis == 236
replace district_outcome = 1 if wahlkreis == 237
replace district_outcome = 1 if wahlkreis == 238
replace district_outcome = 1 if wahlkreis == 239
replace district_outcome = 1 if wahlkreis == 240
replace district_outcome = 1 if wahlkreis == 241
replace district_outcome = 1 if wahlkreis == 242
replace district_outcome = 1 if wahlkreis == 243
replace district_outcome = 1 if wahlkreis == 244
replace district_outcome = 1 if wahlkreis == 245
replace district_outcome = 1 if wahlkreis == 246
replace district_outcome = 1 if wahlkreis == 247
replace district_outcome = 1 if wahlkreis == 248
replace district_outcome = 1 if wahlkreis == 249
replace district_outcome = 1 if wahlkreis == 250
replace district_outcome = 1 if wahlkreis == 251
replace district_outcome = 1 if wahlkreis == 252
replace district_outcome = 1 if wahlkreis == 253
replace district_outcome = 1 if wahlkreis == 254
replace district_outcome = 1 if wahlkreis == 255
replace district_outcome = 1 if wahlkreis == 256
replace district_outcome = 1 if wahlkreis == 257
replace district_outcome = 1 if wahlkreis == 258
replace district_outcome = 1 if wahlkreis == 259
replace district_outcome = 1 if wahlkreis == 260
replace district_outcome = 1 if wahlkreis == 261
replace district_outcome = 1 if wahlkreis == 262
replace district_outcome = 1 if wahlkreis == 263
replace district_outcome = 1 if wahlkreis == 264
replace district_outcome = 1 if wahlkreis == 265
replace district_outcome = 1 if wahlkreis == 266
replace district_outcome = 1 if wahlkreis == 267
replace district_outcome = 1 if wahlkreis == 268
replace district_outcome = 1 if wahlkreis == 269
replace district_outcome = 1 if wahlkreis == 270
replace district_outcome = 1 if wahlkreis == 271
replace district_outcome = 1 if wahlkreis == 272
replace district_outcome = 1 if wahlkreis == 273
replace district_outcome = 1 if wahlkreis == 274
replace district_outcome = 1 if wahlkreis == 275
replace district_outcome = 1 if wahlkreis == 276
replace district_outcome = 1 if wahlkreis == 277
replace district_outcome = 1 if wahlkreis == 278
replace district_outcome = 1 if wahlkreis == 279
replace district_outcome = 1 if wahlkreis == 280
replace district_outcome = 1 if wahlkreis == 281
replace district_outcome = 1 if wahlkreis == 282
replace district_outcome = 1 if wahlkreis == 283
replace district_outcome = 1 if wahlkreis == 284
replace district_outcome = 1 if wahlkreis == 285
replace district_outcome = 1 if wahlkreis == 286
replace district_outcome = 1 if wahlkreis == 287
replace district_outcome = 1 if wahlkreis == 288
replace district_outcome = 1 if wahlkreis == 289
replace district_outcome = 1 if wahlkreis == 290
replace district_outcome = 1 if wahlkreis == 291
replace district_outcome = 1 if wahlkreis == 292
replace district_outcome = 1 if wahlkreis == 293
replace district_outcome = 1 if wahlkreis == 294
replace district_outcome = 1 if wahlkreis == 295
replace district_outcome = 4 if wahlkreis == 296
replace district_outcome = 1 if wahlkreis == 297
replace district_outcome = 1 if wahlkreis == 298
replace district_outcome = 1 if wahlkreis == 299


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 11.98288 if wahlkreis == 1
replace margin = 19.99391 if wahlkreis == 2
replace margin = 15.7339 if wahlkreis == 3
replace margin = 13.80828 if wahlkreis == 4
replace margin = .284153 if wahlkreis == 5
replace margin = 11.7872 if wahlkreis == 6
replace margin = 9.326422 if wahlkreis == 7
replace margin = 13.76114 if wahlkreis == 8
replace margin = 10.75396 if wahlkreis == 9
replace margin = 12.289 if wahlkreis == 10
replace margin = 1.440189 if wahlkreis == 11
replace margin = 9.971502 if wahlkreis == 12
replace margin = 6.03985 if wahlkreis == 13
replace margin = 4.759029 if wahlkreis == 14
replace margin = 24.79572 if wahlkreis == 15
replace margin = 7.757975 if wahlkreis == 16
replace margin = 19.20197 if wahlkreis == 17
replace margin = 6.692526 if wahlkreis == 18
replace margin = 3.016649 if wahlkreis == 19
replace margin = 2.910877 if wahlkreis == 20
replace margin = 2.675869 if wahlkreis == 21
replace margin = 4.836512 if wahlkreis == 22
replace margin = 6.61516 if wahlkreis == 23
replace margin = 21.99393 if wahlkreis == 24
replace margin = 21.99624 if wahlkreis == 25
replace margin = 7.257015 if wahlkreis == 26
replace margin = 6.10725 if wahlkreis == 27
replace margin = 1.22691 if wahlkreis == 28
replace margin = 12.01271 if wahlkreis == 29
replace margin = 16.22238 if wahlkreis == 30
replace margin = 27.26845 if wahlkreis == 31
replace margin = 37.34788 if wahlkreis == 32
replace margin = 17.27513 if wahlkreis == 33
replace margin = 7.284145 if wahlkreis == 34
replace margin = 5.097664 if wahlkreis == 35
replace margin = 13.21147 if wahlkreis == 36
replace margin = 5.422653 if wahlkreis == 37
replace margin = 17.28524 if wahlkreis == 38
replace margin = 8.638092 if wahlkreis == 39
replace margin = 7.913967 if wahlkreis == 40
replace margin = 6.042797 if wahlkreis == 41
replace margin = 4.762777 if wahlkreis == 42
replace margin = 7.068451 if wahlkreis == 43
replace margin = 12.72793 if wahlkreis == 44
replace margin = 1.761932 if wahlkreis == 45
replace margin = 5.740635 if wahlkreis == 46
replace margin = 1.761024 if wahlkreis == 47
replace margin = 1.1861 if wahlkreis == 48
replace margin = 13.68481 if wahlkreis == 49
replace margin = 6.668943 if wahlkreis == 50
replace margin = 3.114143 if wahlkreis == 51
replace margin = 4.979515 if wahlkreis == 52
replace margin = 1.645103 if wahlkreis == 53
replace margin = 5.793882 if wahlkreis == 54
replace margin = 8.996881 if wahlkreis == 55
replace margin = 7.110586 if wahlkreis == 56
replace margin = 10.39914 if wahlkreis == 57
replace margin = 7.149048 if wahlkreis == 58
replace margin = 5.890636 if wahlkreis == 59
replace margin = 6.711554 if wahlkreis == 60
replace margin = 1.288305 if wahlkreis == 61
replace margin = 10.43997 if wahlkreis == 62
replace margin = 5.134134 if wahlkreis == 63
replace margin = 3.144392 if wahlkreis == 64
replace margin = 4.801899 if wahlkreis == 65
replace margin = 13.40368 if wahlkreis == 66
replace margin = 17.46787 if wahlkreis == 67
replace margin = 17.20566 if wahlkreis == 68
replace margin = 5.69521 if wahlkreis == 69
replace margin = 15.86586 if wahlkreis == 70
replace margin = 9.340734 if wahlkreis == 71
replace margin = 5.747463 if wahlkreis == 72
replace margin = 10.19431 if wahlkreis == 73
replace margin = 7.163458 if wahlkreis == 74
replace margin = 3.048212 if wahlkreis == 75
replace margin = 9.225872 if wahlkreis == 76
replace margin = 13.17811 if wahlkreis == 77
replace margin = 1.219997 if wahlkreis == 78
replace margin = 10.81105 if wahlkreis == 79
replace margin = 2.532764 if wahlkreis == 80
replace margin = 6.900026 if wahlkreis == 81
replace margin = 2.256477 if wahlkreis == 82
replace margin = 1.432749 if wahlkreis == 83
replace margin = 20.93987 if wahlkreis == 84
replace margin = 11.91797 if wahlkreis == 85
replace margin = 15.19256 if wahlkreis == 86
replace margin = 1.221325 if wahlkreis == 87
replace margin = .4264603 if wahlkreis == 88
replace margin = 17.65241 if wahlkreis == 89
replace margin = 10.12293 if wahlkreis == 90
replace margin = 8.141481 if wahlkreis == 91
replace margin = 16.65149 if wahlkreis == 92
replace margin = .6396713 if wahlkreis == 93
replace margin = 8.018356 if wahlkreis == 94
replace margin = 4.723068 if wahlkreis == 95
replace margin = 2.82048 if wahlkreis == 96
replace margin = 16.61414 if wahlkreis == 97
replace margin = 23.82619 if wahlkreis == 98
replace margin = 17.00694 if wahlkreis == 99
replace margin = 15.58871 if wahlkreis == 100
replace margin = 7.882359 if wahlkreis == 101
replace margin = 1.920868 if wahlkreis == 102
replace margin = 7.423254 if wahlkreis == 103
replace margin = 18.88552 if wahlkreis == 104
replace margin = 9.050215 if wahlkreis == 105
replace margin = 15.96971 if wahlkreis == 106
replace margin = 6.502947 if wahlkreis == 107
replace margin = 15.37918 if wahlkreis == 108
replace margin = 19.93208 if wahlkreis == 109
replace margin = 16.975 if wahlkreis == 110
replace margin = 22.86439 if wahlkreis == 111
replace margin = 14.44472 if wahlkreis == 112
replace margin = 6.153225 if wahlkreis == 113
replace margin = 4.994812 if wahlkreis == 114
replace margin = 9.600231 if wahlkreis == 115
replace margin = 8.251583 if wahlkreis == 116
replace margin = 9.39097 if wahlkreis == 117
replace margin = 3.549452 if wahlkreis == 118
replace margin = 10.74811 if wahlkreis == 119
replace margin = 6.287804 if wahlkreis == 120
replace margin = 7.742838 if wahlkreis == 121
replace margin = 6.637676 if wahlkreis == 122
replace margin = 12.9866 if wahlkreis == 123
replace margin = 25.45665 if wahlkreis == 124
replace margin = 3.199005 if wahlkreis == 125
replace margin = 27.01658 if wahlkreis == 126
replace margin = 28.13012 if wahlkreis == 127
replace margin = 14.47376 if wahlkreis == 128
replace margin = 8.336931 if wahlkreis == 129
replace margin = 18.51831 if wahlkreis == 130
replace margin = 18.52874 if wahlkreis == 131
replace margin = 2.499496 if wahlkreis == 132
replace margin = .9808388 if wahlkreis == 133
replace margin = 1.87989 if wahlkreis == 134
replace margin = 4.413994 if wahlkreis == 135
replace margin = 17.54723 if wahlkreis == 136
replace margin = 33.41703 if wahlkreis == 137
replace margin = 8.940046 if wahlkreis == 138
replace margin = 3.765553 if wahlkreis == 139
replace margin = 8.955147 if wahlkreis == 140
replace margin = 17.74319 if wahlkreis == 141
replace margin = 10.20022 if wahlkreis == 142
replace margin = 10.64564 if wahlkreis == 143
replace margin = 7.073668 if wahlkreis == 144
replace margin = 1.189491 if wahlkreis == 145
replace margin = 13.41338 if wahlkreis == 146
replace margin = 21.05822 if wahlkreis == 147
replace margin = 9.909084 if wahlkreis == 148
replace margin = 21.53024 if wahlkreis == 149
replace margin = .8276291 if wahlkreis == 150
replace margin = 5.980007 if wahlkreis == 151
replace margin = 7.020233 if wahlkreis == 152
replace margin = .6738224 if wahlkreis == 153
replace margin = 5.401749 if wahlkreis == 154
replace margin = 5.672091 if wahlkreis == 155
replace margin = 2.637905 if wahlkreis == 156
replace margin = 1.027056 if wahlkreis == 157
replace margin = 8.577055 if wahlkreis == 158
replace margin = 2.218691 if wahlkreis == 159
replace margin = 3.239429 if wahlkreis == 160
replace margin = .9436779 if wahlkreis == 161
replace margin = 2.538593 if wahlkreis == 162
replace margin = 8.505159 if wahlkreis == 163
replace margin = 4.433451 if wahlkreis == 164
replace margin = 7.996931 if wahlkreis == 165
replace margin = 9.031385 if wahlkreis == 166
replace margin = 1.473286 if wahlkreis == 167
replace margin = 8.672661 if wahlkreis == 168
replace margin = 11.22516 if wahlkreis == 169
replace margin = 7.25293 if wahlkreis == 170
replace margin = 2.36615 if wahlkreis == 171
replace margin = 8.549105 if wahlkreis == 172
replace margin = 6.940874 if wahlkreis == 173
replace margin = 24.98195 if wahlkreis == 174
replace margin = 8.108545 if wahlkreis == 175
replace margin = 16.89865 if wahlkreis == 176
replace margin = 7.389664 if wahlkreis == 177
replace margin = 16.41667 if wahlkreis == 178
replace margin = 5.735512 if wahlkreis == 179
replace margin = 4.900082 if wahlkreis == 180
replace margin = 20.16275 if wahlkreis == 181
replace margin = 3.466644 if wahlkreis == 182
replace margin = 6.437862 if wahlkreis == 183
replace margin = 2.767143 if wahlkreis == 184
replace margin = 11.46643 if wahlkreis == 185
replace margin = .9347916 if wahlkreis == 186
replace margin = 7.090651 if wahlkreis == 187
replace margin = 11.95078 if wahlkreis == 188
replace margin = 16.60484 if wahlkreis == 189
replace margin = 13.25914 if wahlkreis == 190
replace margin = 7.851139 if wahlkreis == 191
replace margin = 5.098391 if wahlkreis == 192
replace margin = 8.652704 if wahlkreis == 193
replace margin = 3.114069 if wahlkreis == 194
replace margin = 4.379635 if wahlkreis == 195
replace margin = 10.70161 if wahlkreis == 196
replace margin = 14.55458 if wahlkreis == 197
replace margin = 15.41629 if wahlkreis == 198
replace margin = 12.6281 if wahlkreis == 199
replace margin = 18.91092 if wahlkreis == 200
replace margin = 5.516932 if wahlkreis == 201
replace margin = 25.48761 if wahlkreis == 202
replace margin = 4.237366 if wahlkreis == 203
replace margin = 13.49427 if wahlkreis == 204
replace margin = 7.663277 if wahlkreis == 205
replace margin = 14.42551 if wahlkreis == 206
replace margin = .2688751 if wahlkreis == 207
replace margin = 14.66259 if wahlkreis == 208
replace margin = 2.63315 if wahlkreis == 209
replace margin = 8.08485 if wahlkreis == 210
replace margin = 14.33084 if wahlkreis == 211
replace margin = 41.3811 if wahlkreis == 212
replace margin = 33.33231 if wahlkreis == 213
replace margin = 29.50097 if wahlkreis == 214
replace margin = 24.9065 if wahlkreis == 215
replace margin = 35.94196 if wahlkreis == 216
replace margin = 6.205484 if wahlkreis == 217
replace margin = 15.42058 if wahlkreis == 218
replace margin = 9.457664 if wahlkreis == 219
replace margin = 10.27244 if wahlkreis == 220
replace margin = 27.22805 if wahlkreis == 221
replace margin = 32.97846 if wahlkreis == 222
replace margin = 34.04011 if wahlkreis == 223
replace margin = 25.40998 if wahlkreis == 224
replace margin = 34.1636 if wahlkreis == 225
replace margin = 33.11986 if wahlkreis == 226
replace margin = 26.65321 if wahlkreis == 227
replace margin = 26.0057 if wahlkreis == 228
replace margin = 28.59623 if wahlkreis == 229
replace margin = 29.92443 if wahlkreis == 230
replace margin = 30.80906 if wahlkreis == 231
replace margin = 32.46648 if wahlkreis == 232
replace margin = 23.36139 if wahlkreis == 233
replace margin = 24.28309 if wahlkreis == 234
replace margin = 23.92601 if wahlkreis == 235
replace margin = 21.64667 if wahlkreis == 236
replace margin = 25.2673 if wahlkreis == 237
replace margin = 18.83017 if wahlkreis == 238
replace margin = 23.47169 if wahlkreis == 239
replace margin = 39.35681 if wahlkreis == 240
replace margin = 25.92454 if wahlkreis == 241
replace margin = 21.61049 if wahlkreis == 242
replace margin = 17.00478 if wahlkreis == 243
replace margin = 5.707859 if wahlkreis == 244
replace margin = 9.098911 if wahlkreis == 245
replace margin = 23.86392 if wahlkreis == 246
replace margin = 31.52635 if wahlkreis == 247
replace margin = 32.00576 if wahlkreis == 248
replace margin = 23.9791 if wahlkreis == 249
replace margin = 30.82602 if wahlkreis == 250
replace margin = 23.48963 if wahlkreis == 251
replace margin = 15.48648 if wahlkreis == 252
replace margin = 33.68042 if wahlkreis == 253
replace margin = 28.81958 if wahlkreis == 254
replace margin = 29.98403 if wahlkreis == 255
replace margin = 38.18901 if wahlkreis == 256
replace margin = 36.55399 if wahlkreis == 257
replace margin = 2.309664 if wahlkreis == 258
replace margin = 15.01556 if wahlkreis == 259
replace margin = 19.18306 if wahlkreis == 260
replace margin = 20.85328 if wahlkreis == 261
replace margin = 20.39305 if wahlkreis == 262
replace margin = 15.62117 if wahlkreis == 263
replace margin = 17.60701 if wahlkreis == 264
replace margin = 20.43334 if wahlkreis == 265
replace margin = 20.26336 if wahlkreis == 266
replace margin = 12.08891 if wahlkreis == 267
replace margin = 21.96574 if wahlkreis == 268
replace margin = 21.23426 if wahlkreis == 269
replace margin = 25.41262 if wahlkreis == 270
replace margin = 4.920065 if wahlkreis == 271
replace margin = 20.69689 if wahlkreis == 272
replace margin = 25.0326 if wahlkreis == 273
replace margin = 6.645266 if wahlkreis == 274
replace margin = 1.414703 if wahlkreis == 275
replace margin = 27.71441 if wahlkreis == 276
replace margin = 13.53616 if wahlkreis == 277
replace margin = 21.97587 if wahlkreis == 278
replace margin = 17.39138 if wahlkreis == 279
replace margin = 26.41309 if wahlkreis == 280
replace margin = 2.308584 if wahlkreis == 281
replace margin = 18.32415 if wahlkreis == 282
replace margin = 13.82817 if wahlkreis == 283
replace margin = 30.86012 if wahlkreis == 284
replace margin = 27.12991 if wahlkreis == 285
replace margin = 30.29913 if wahlkreis == 286
replace margin = 27.97994 if wahlkreis == 287
replace margin = 17.77445 if wahlkreis == 288
replace margin = 25.73571 if wahlkreis == 289
replace margin = 16.59376 if wahlkreis == 290
replace margin = 22.45332 if wahlkreis == 291
replace margin = 27.52341 if wahlkreis == 292
replace margin = 23.35449 if wahlkreis == 293
replace margin = 18.34823 if wahlkreis == 294
replace margin = 30.59755 if wahlkreis == 295
replace margin = .7047596 if wahlkreis == 296
replace margin = 5.967789 if wahlkreis == 297
replace margin = 11.42762 if wahlkreis == 298
replace margin = 2.149023 if wahlkreis == 299

* Effective number of electoral parties by district

gen enep = .

replace enep = 3.804072 if wahlkreis == 1
replace enep = 3.463197 if wahlkreis == 2
replace enep = 3.712399 if wahlkreis == 3
replace enep = 3.506922 if wahlkreis == 4
replace enep = 4.42103 if wahlkreis == 5
replace enep = 3.681591 if wahlkreis == 6
replace enep = 3.674201 if wahlkreis == 7
replace enep = 3.740649 if wahlkreis == 8
replace enep = 3.504245 if wahlkreis == 9
replace enep = 3.88711 if wahlkreis == 10
replace enep = 3.728021 if wahlkreis == 11
replace enep = 4.73648 if wahlkreis == 12
replace enep = 4.777366 if wahlkreis == 13
replace enep = 4.821718 if wahlkreis == 14
replace enep = 3.686105 if wahlkreis == 15
replace enep = 4.702591 if wahlkreis == 16
replace enep = 4.276604 if wahlkreis == 17
replace enep = 4.992776 if wahlkreis == 18
replace enep = 4.981019 if wahlkreis == 19
replace enep = 4.469752 if wahlkreis == 20
replace enep = 4.140117 if wahlkreis == 21
replace enep = 4.24051 if wahlkreis == 22
replace enep = 4.309796 if wahlkreis == 23
replace enep = 2.98577 if wahlkreis == 24
replace enep = 2.936002 if wahlkreis == 25
replace enep = 3.574759 if wahlkreis == 26
replace enep = 3.987971 if wahlkreis == 27
replace enep = 3.991036 if wahlkreis == 28
replace enep = 3.406329 if wahlkreis == 29
replace enep = 3.391109 if wahlkreis == 30
replace enep = 2.725194 if wahlkreis == 31
replace enep = 2.58592 if wahlkreis == 32
replace enep = 3.405647 if wahlkreis == 33
replace enep = 3.623575 if wahlkreis == 34
replace enep = 3.197183 if wahlkreis == 35
replace enep = 3.780373 if wahlkreis == 36
replace enep = 4.318802 if wahlkreis == 37
replace enep = 3.281053 if wahlkreis == 38
replace enep = 3.476093 if wahlkreis == 39
replace enep = 3.43993 if wahlkreis == 40
replace enep = 4.167012 if wahlkreis == 41
replace enep = 4.394605 if wahlkreis == 42
replace enep = 3.46748 if wahlkreis == 43
replace enep = 3.434817 if wahlkreis == 44
replace enep = 3.436963 if wahlkreis == 45
replace enep = 3.533944 if wahlkreis == 46
replace enep = 3.581253 if wahlkreis == 47
replace enep = 3.485778 if wahlkreis == 48
replace enep = 3.444935 if wahlkreis == 49
replace enep = 3.783807 if wahlkreis == 50
replace enep = 3.499612 if wahlkreis == 51
replace enep = 3.373088 if wahlkreis == 52
replace enep = 3.905467 if wahlkreis == 53
replace enep = 5.077059 if wahlkreis == 54
replace enep = 4.650313 if wahlkreis == 55
replace enep = 4.609753 if wahlkreis == 56
replace enep = 4.774794 if wahlkreis == 57
replace enep = 4.975107 if wahlkreis == 58
replace enep = 4.932113 if wahlkreis == 59
replace enep = 4.560237 if wahlkreis == 60
replace enep = 5.399308 if wahlkreis == 61
replace enep = 4.859138 if wahlkreis == 62
replace enep = 5.214614 if wahlkreis == 63
replace enep = 4.918558 if wahlkreis == 64
replace enep = 4.823287 if wahlkreis == 65
replace enep = 4.707846 if wahlkreis == 66
replace enep = 4.214524 if wahlkreis == 67
replace enep = 4.342418 if wahlkreis == 68
replace enep = 5.306391 if wahlkreis == 69
replace enep = 4.639732 if wahlkreis == 70
replace enep = 4.634105 if wahlkreis == 71
replace enep = 5.090042 if wahlkreis == 72
replace enep = 4.568019 if wahlkreis == 73
replace enep = 4.625332 if wahlkreis == 74
replace enep = 5.71312 if wahlkreis == 75
replace enep = 5.395815 if wahlkreis == 76
replace enep = 4.411691 if wahlkreis == 77
replace enep = 4.326249 if wahlkreis == 78
replace enep = 4.493749 if wahlkreis == 79
replace enep = 4.776031 if wahlkreis == 80
replace enep = 5.191568 if wahlkreis == 81
replace enep = 5.366517 if wahlkreis == 82
replace enep = 5.407489 if wahlkreis == 83
replace enep = 4.153781 if wahlkreis == 84
replace enep = 4.395872 if wahlkreis == 85
replace enep = 4.753105 if wahlkreis == 86
replace enep = 4.081565 if wahlkreis == 87
replace enep = 3.499454 if wahlkreis == 88
replace enep = 3.309971 if wahlkreis == 89
replace enep = 3.420019 if wahlkreis == 90
replace enep = 3.675375 if wahlkreis == 91
replace enep = 3.602698 if wahlkreis == 92
replace enep = 4.408132 if wahlkreis == 93
replace enep = 4.304329 if wahlkreis == 94
replace enep = 4.580397 if wahlkreis == 95
replace enep = 4.009779 if wahlkreis == 96
replace enep = 3.384932 if wahlkreis == 97
replace enep = 3.431324 if wahlkreis == 98
replace enep = 3.504774 if wahlkreis == 99
replace enep = 3.889987 if wahlkreis == 100
replace enep = 3.786911 if wahlkreis == 101
replace enep = 4.53396 if wahlkreis == 102
replace enep = 3.791022 if wahlkreis == 103
replace enep = 3.47077 if wahlkreis == 104
replace enep = 3.730186 if wahlkreis == 105
replace enep = 3.905032 if wahlkreis == 106
replace enep = 4.388874 if wahlkreis == 107
replace enep = 3.401892 if wahlkreis == 108
replace enep = 3.592118 if wahlkreis == 109
replace enep = 3.693527 if wahlkreis == 110
replace enep = 3.217857 if wahlkreis == 111
replace enep = 3.218236 if wahlkreis == 112
replace enep = 3.571707 if wahlkreis == 113
replace enep = 3.848083 if wahlkreis == 114
replace enep = 3.923074 if wahlkreis == 115
replace enep = 4.293303 if wahlkreis == 116
replace enep = 3.835429 if wahlkreis == 117
replace enep = 4.008425 if wahlkreis == 118
replace enep = 4.025688 if wahlkreis == 119
replace enep = 3.895386 if wahlkreis == 120
replace enep = 3.703208 if wahlkreis == 121
replace enep = 3.244297 if wahlkreis == 122
replace enep = 3.987678 if wahlkreis == 123
replace enep = 2.914147 if wahlkreis == 124
replace enep = 3.672707 if wahlkreis == 125
replace enep = 2.832712 if wahlkreis == 126
replace enep = 2.914529 if wahlkreis == 127
replace enep = 3.25918 if wahlkreis == 128
replace enep = 4.004393 if wahlkreis == 129
replace enep = 3.241328 if wahlkreis == 130
replace enep = 3.213341 if wahlkreis == 131
replace enep = 4.273833 if wahlkreis == 132
replace enep = 3.547757 if wahlkreis == 133
replace enep = 3.502829 if wahlkreis == 134
replace enep = 3.829506 if wahlkreis == 135
replace enep = 3.479121 if wahlkreis == 136
replace enep = 2.924722 if wahlkreis == 137
replace enep = 3.708704 if wahlkreis == 138
replace enep = 3.717089 if wahlkreis == 139
replace enep = 4.032199 if wahlkreis == 140
replace enep = 3.764616 if wahlkreis == 141
replace enep = 3.898341 if wahlkreis == 142
replace enep = 3.896625 if wahlkreis == 143
replace enep = 3.685526 if wahlkreis == 144
replace enep = 3.61898 if wahlkreis == 145
replace enep = 3.472412 if wahlkreis == 146
replace enep = 3.140546 if wahlkreis == 147
replace enep = 3.66502 if wahlkreis == 148
replace enep = 3.150885 if wahlkreis == 149
replace enep = 3.212372 if wahlkreis == 150
replace enep = 4.278409 if wahlkreis == 151
replace enep = 5.272968 if wahlkreis == 152
replace enep = 5.547959 if wahlkreis == 153
replace enep = 4.147927 if wahlkreis == 154
replace enep = 3.867563 if wahlkreis == 155
replace enep = 4.143551 if wahlkreis == 156
replace enep = 4.194272 if wahlkreis == 157
replace enep = 3.92042 if wahlkreis == 158
replace enep = 5.479798 if wahlkreis == 159
replace enep = 5.821369 if wahlkreis == 160
replace enep = 4.118867 if wahlkreis == 161
replace enep = 5.095618 if wahlkreis == 162
replace enep = 4.15587 if wahlkreis == 163
replace enep = 4.018447 if wahlkreis == 164
replace enep = 4.428035 if wahlkreis == 165
replace enep = 4.316685 if wahlkreis == 166
replace enep = 3.841754 if wahlkreis == 167
replace enep = 4.369868 if wahlkreis == 168
replace enep = 3.541341 if wahlkreis == 169
replace enep = 3.85077 if wahlkreis == 170
replace enep = 3.813105 if wahlkreis == 171
replace enep = 3.865092 if wahlkreis == 172
replace enep = 4.293839 if wahlkreis == 173
replace enep = 3.521968 if wahlkreis == 174
replace enep = 4.073808 if wahlkreis == 175
replace enep = 4.071181 if wahlkreis == 176
replace enep = 4.076944 if wahlkreis == 177
replace enep = 3.796264 if wahlkreis == 178
replace enep = 4.363691 if wahlkreis == 179
replace enep = 4.075188 if wahlkreis == 180
replace enep = 3.912849 if wahlkreis == 181
replace enep = 4.882829 if wahlkreis == 182
replace enep = 4.728322 if wahlkreis == 183
replace enep = 3.945402 if wahlkreis == 184
replace enep = 4.40141 if wahlkreis == 185
replace enep = 4.500326 if wahlkreis == 186
replace enep = 4.104287 if wahlkreis == 187
replace enep = 3.936102 if wahlkreis == 188
replace enep = 4.208632 if wahlkreis == 189
replace enep = 4.64274 if wahlkreis == 190
replace enep = 5.109492 if wahlkreis == 191
replace enep = 4.850485 if wahlkreis == 192
replace enep = 5.480324 if wahlkreis == 193
replace enep = 4.536784 if wahlkreis == 194
replace enep = 4.660597 if wahlkreis == 195
replace enep = 4.547696 if wahlkreis == 196
replace enep = 3.483567 if wahlkreis == 197
replace enep = 3.586941 if wahlkreis == 198
replace enep = 3.686858 if wahlkreis == 199
replace enep = 3.592258 if wahlkreis == 200
replace enep = 3.86905 if wahlkreis == 201
replace enep = 2.92144 if wahlkreis == 202
replace enep = 3.655367 if wahlkreis == 203
replace enep = 3.38439 if wahlkreis == 204
replace enep = 4.298972 if wahlkreis == 205
replace enep = 3.791164 if wahlkreis == 206
replace enep = 4.193466 if wahlkreis == 207
replace enep = 3.967197 if wahlkreis == 208
replace enep = 4.150155 if wahlkreis == 209
replace enep = 4.068885 if wahlkreis == 210
replace enep = 3.869518 if wahlkreis == 211
replace enep = 2.944209 if wahlkreis == 212
replace enep = 3.523602 if wahlkreis == 213
replace enep = 4.174765 if wahlkreis == 214
replace enep = 3.958417 if wahlkreis == 215
replace enep = 3.429257 if wahlkreis == 216
replace enep = 4.79975 if wahlkreis == 217
replace enep = 4.509858 if wahlkreis == 218
replace enep = 4.911661 if wahlkreis == 219
replace enep = 4.779691 if wahlkreis == 220
replace enep = 3.948558 if wahlkreis == 221
replace enep = 3.847475 if wahlkreis == 222
replace enep = 3.589045 if wahlkreis == 223
replace enep = 4.183387 if wahlkreis == 224
replace enep = 3.306992 if wahlkreis == 225
replace enep = 3.57924 if wahlkreis == 226
replace enep = 3.793052 if wahlkreis == 227
replace enep = 4.581124 if wahlkreis == 228
replace enep = 3.43342 if wahlkreis == 229
replace enep = 3.873002 if wahlkreis == 230
replace enep = 3.505542 if wahlkreis == 231
replace enep = 3.613563 if wahlkreis == 232
replace enep = 4.478342 if wahlkreis == 233
replace enep = 3.228183 if wahlkreis == 234
replace enep = 3.539277 if wahlkreis == 235
replace enep = 4.021199 if wahlkreis == 236
replace enep = 3.536319 if wahlkreis == 237
replace enep = 3.397254 if wahlkreis == 238
replace enep = 3.366534 if wahlkreis == 239
replace enep = 2.834323 if wahlkreis == 240
replace enep = 3.893482 if wahlkreis == 241
replace enep = 3.956413 if wahlkreis == 242
replace enep = 4.142818 if wahlkreis == 243
replace enep = 4.903269 if wahlkreis == 244
replace enep = 4.321447 if wahlkreis == 245
replace enep = 3.788229 if wahlkreis == 246
replace enep = 3.482358 if wahlkreis == 247
replace enep = 3.12454 if wahlkreis == 248
replace enep = 3.474584 if wahlkreis == 249
replace enep = 3.486739 if wahlkreis == 250
replace enep = 4.017741 if wahlkreis == 251
replace enep = 4.832576 if wahlkreis == 252
replace enep = 3.587351 if wahlkreis == 253
replace enep = 3.558454 if wahlkreis == 254
replace enep = 3.906632 if wahlkreis == 255
replace enep = 3.373215 if wahlkreis == 256
replace enep = 3.49194 if wahlkreis == 257
replace enep = 4.478646 if wahlkreis == 258
replace enep = 5.046672 if wahlkreis == 259
replace enep = 4.264761 if wahlkreis == 260
replace enep = 4.120584 if wahlkreis == 261
replace enep = 4.171345 if wahlkreis == 262
replace enep = 4.248886 if wahlkreis == 263
replace enep = 4.481893 if wahlkreis == 264
replace enep = 4.425269 if wahlkreis == 265
replace enep = 4.163527 if wahlkreis == 266
replace enep = 4.528573 if wahlkreis == 267
replace enep = 4.147614 if wahlkreis == 268
replace enep = 4.00039 if wahlkreis == 269
replace enep = 3.48335 if wahlkreis == 270
replace enep = 5.205498 if wahlkreis == 271
replace enep = 4.169589 if wahlkreis == 272
replace enep = 3.775467 if wahlkreis == 273
replace enep = 4.572658 if wahlkreis == 274
replace enep = 4.822037 if wahlkreis == 275
replace enep = 3.483111 if wahlkreis == 276
replace enep = 4.29088 if wahlkreis == 277
replace enep = 4.081946 if wahlkreis == 278
replace enep = 4.543095 if wahlkreis == 279
replace enep = 3.928986 if wahlkreis == 280
replace enep = 4.764551 if wahlkreis == 281
replace enep = 4.141908 if wahlkreis == 282
replace enep = 4.309174 if wahlkreis == 283
replace enep = 3.390482 if wahlkreis == 284
replace enep = 3.996503 if wahlkreis == 285
replace enep = 3.563702 if wahlkreis == 286
replace enep = 3.757505 if wahlkreis == 287
replace enep = 3.813765 if wahlkreis == 288
replace enep = 4.210583 if wahlkreis == 289
replace enep = 4.643925 if wahlkreis == 290
replace enep = 3.883621 if wahlkreis == 291
replace enep = 3.777585 if wahlkreis == 292
replace enep = 4.094195 if wahlkreis == 293
replace enep = 4.396269 if wahlkreis == 294
replace enep = 3.727102 if wahlkreis == 295
replace enep = 4.269126 if wahlkreis == 296
replace enep = 3.699452 if wahlkreis == 297
replace enep = 3.484266 if wahlkreis == 298
replace enep = 4.149811 if wahlkreis == 299


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if wahlkreis == 1
replace reelected = 1 if wahlkreis == 2
replace reelected = 1 if wahlkreis == 3
replace reelected = 1 if wahlkreis == 4
replace reelected = 1 if wahlkreis == 5
replace reelected = 1 if wahlkreis == 6
replace reelected = 1 if wahlkreis == 7
replace reelected = 1 if wahlkreis == 8
replace reelected = 1 if wahlkreis == 9
replace reelected = 1 if wahlkreis == 10
replace reelected = 0 if wahlkreis == 11
replace reelected = 1 if wahlkreis == 12
replace reelected = 1 if wahlkreis == 13
replace reelected = 1 if wahlkreis == 14
replace reelected = 1 if wahlkreis == 15
replace reelected = 1 if wahlkreis == 16
replace reelected = 1 if wahlkreis == 17
replace reelected = 1 if wahlkreis == 18
replace reelected = 1 if wahlkreis == 19
replace reelected = 1 if wahlkreis == 20
replace reelected = 1 if wahlkreis == 21
replace reelected = 1 if wahlkreis == 22
replace reelected = 1 if wahlkreis == 23
replace reelected = 1 if wahlkreis == 24
replace reelected = 1 if wahlkreis == 25
replace reelected = 1 if wahlkreis == 26
replace reelected = 1 if wahlkreis == 27
replace reelected = 1 if wahlkreis == 28
replace reelected = 1 if wahlkreis == 29
replace reelected = 1 if wahlkreis == 30
replace reelected = 1 if wahlkreis == 31
replace reelected = 1 if wahlkreis == 32
replace reelected = 1 if wahlkreis == 33
replace reelected = 1 if wahlkreis == 34
replace reelected = 0 if wahlkreis == 35
replace reelected = 1 if wahlkreis == 36
replace reelected = 1 if wahlkreis == 37
replace reelected = 1 if wahlkreis == 38
replace reelected = 1 if wahlkreis == 39
replace reelected = 0 if wahlkreis == 40
replace reelected = 1 if wahlkreis == 41
replace reelected = 1 if wahlkreis == 42
replace reelected = 1 if wahlkreis == 43
replace reelected = 1 if wahlkreis == 44
replace reelected = 1 if wahlkreis == 45
replace reelected = 1 if wahlkreis == 46
replace reelected = 1 if wahlkreis == 47
replace reelected = 0 if wahlkreis == 48
replace reelected = 1 if wahlkreis == 49
replace reelected = 1 if wahlkreis == 50
replace reelected = 0 if wahlkreis == 51
replace reelected = 0 if wahlkreis == 52
replace reelected = 1 if wahlkreis == 53
replace reelected = 1 if wahlkreis == 54
replace reelected = 1 if wahlkreis == 55
replace reelected = 1 if wahlkreis == 56
replace reelected = 1 if wahlkreis == 57
replace reelected = 1 if wahlkreis == 58
replace reelected = 1 if wahlkreis == 59
replace reelected = 0 if wahlkreis == 60
replace reelected = 1 if wahlkreis == 61
replace reelected = 1 if wahlkreis == 62
replace reelected = 1 if wahlkreis == 63
replace reelected = 1 if wahlkreis == 64
replace reelected = 1 if wahlkreis == 65
replace reelected = 1 if wahlkreis == 66
replace reelected = 1 if wahlkreis == 67
replace reelected = 1 if wahlkreis == 68
replace reelected = 1 if wahlkreis == 69
replace reelected = 1 if wahlkreis == 70
replace reelected = 1 if wahlkreis == 71
replace reelected = 1 if wahlkreis == 72
replace reelected = 1 if wahlkreis == 73
replace reelected = 1 if wahlkreis == 74
replace reelected = 1 if wahlkreis == 75
replace reelected = 1 if wahlkreis == 76
replace reelected = 1 if wahlkreis == 77
replace reelected = 0 if wahlkreis == 78
replace reelected = 1 if wahlkreis == 79
replace reelected = 1 if wahlkreis == 80
replace reelected = 1 if wahlkreis == 81
replace reelected = 1 if wahlkreis == 82
replace reelected = 1 if wahlkreis == 83
replace reelected = 1 if wahlkreis == 84
replace reelected = 1 if wahlkreis == 85
replace reelected = 1 if wahlkreis == 86
replace reelected = 1 if wahlkreis == 87
replace reelected = 0 if wahlkreis == 88
replace reelected = 1 if wahlkreis == 89
replace reelected = 1 if wahlkreis == 90
replace reelected = 1 if wahlkreis == 91
replace reelected = 1 if wahlkreis == 92
replace reelected = 0 if wahlkreis == 93
replace reelected = 1 if wahlkreis == 94
replace reelected = 1 if wahlkreis == 95
replace reelected = 1 if wahlkreis == 96
replace reelected = 1 if wahlkreis == 97
replace reelected = 1 if wahlkreis == 98
replace reelected = 1 if wahlkreis == 99
replace reelected = 1 if wahlkreis == 100
replace reelected = 1 if wahlkreis == 101
replace reelected = 1 if wahlkreis == 102
replace reelected = 1 if wahlkreis == 103
replace reelected = 1 if wahlkreis == 104
replace reelected = 1 if wahlkreis == 105
replace reelected = 1 if wahlkreis == 106
replace reelected = 1 if wahlkreis == 107
replace reelected = 1 if wahlkreis == 108
replace reelected = 1 if wahlkreis == 109
replace reelected = 1 if wahlkreis == 110
replace reelected = 1 if wahlkreis == 111
replace reelected = 1 if wahlkreis == 112
replace reelected = 1 if wahlkreis == 113
replace reelected = 0 if wahlkreis == 114
replace reelected = 1 if wahlkreis == 115
replace reelected = 1 if wahlkreis == 116
replace reelected = 1 if wahlkreis == 117
replace reelected = 1 if wahlkreis == 118
replace reelected = 1 if wahlkreis == 119
replace reelected = 1 if wahlkreis == 120
replace reelected = 1 if wahlkreis == 121
replace reelected = 1 if wahlkreis == 122
replace reelected = 1 if wahlkreis == 123
replace reelected = 1 if wahlkreis == 124
replace reelected = 1 if wahlkreis == 125
replace reelected = 1 if wahlkreis == 126
replace reelected = 1 if wahlkreis == 127
replace reelected = 1 if wahlkreis == 128
replace reelected = 1 if wahlkreis == 129
replace reelected = 1 if wahlkreis == 130
replace reelected = 1 if wahlkreis == 131
replace reelected = 1 if wahlkreis == 132
replace reelected = 1 if wahlkreis == 133
replace reelected = 0 if wahlkreis == 134
replace reelected = 0 if wahlkreis == 135
replace reelected = 1 if wahlkreis == 136
replace reelected = 1 if wahlkreis == 137
replace reelected = 1 if wahlkreis == 138
replace reelected = 1 if wahlkreis == 139
replace reelected = 1 if wahlkreis == 140
replace reelected = 1 if wahlkreis == 141
replace reelected = 1 if wahlkreis == 142
replace reelected = 1 if wahlkreis == 143
replace reelected = 1 if wahlkreis == 144
replace reelected = 1 if wahlkreis == 145
replace reelected = 1 if wahlkreis == 146
replace reelected = 1 if wahlkreis == 147
replace reelected = 1 if wahlkreis == 148
replace reelected = 1 if wahlkreis == 149
replace reelected = 1 if wahlkreis == 150
replace reelected = 1 if wahlkreis == 151
replace reelected = 1 if wahlkreis == 152
replace reelected = 0 if wahlkreis == 153
replace reelected = 1 if wahlkreis == 154
replace reelected = 1 if wahlkreis == 155
replace reelected = 0 if wahlkreis == 156
replace reelected = 0 if wahlkreis == 157
replace reelected = 0 if wahlkreis == 158
replace reelected = 1 if wahlkreis == 159
replace reelected = 1 if wahlkreis == 160
replace reelected = 1 if wahlkreis == 161
replace reelected = 1 if wahlkreis == 162
replace reelected = 1 if wahlkreis == 163
replace reelected = 1 if wahlkreis == 164
replace reelected = 1 if wahlkreis == 165
replace reelected = 1 if wahlkreis == 166
replace reelected = 0 if wahlkreis == 167
replace reelected = 1 if wahlkreis == 168
replace reelected = 1 if wahlkreis == 169
replace reelected = 1 if wahlkreis == 170
replace reelected = 1 if wahlkreis == 171
replace reelected = 1 if wahlkreis == 172
replace reelected = 1 if wahlkreis == 173
replace reelected = 1 if wahlkreis == 174
replace reelected = 1 if wahlkreis == 175
replace reelected = 1 if wahlkreis == 176
replace reelected = 1 if wahlkreis == 177
replace reelected = 1 if wahlkreis == 178
replace reelected = 1 if wahlkreis == 179
replace reelected = 1 if wahlkreis == 180
replace reelected = 1 if wahlkreis == 181
replace reelected = 1 if wahlkreis == 182
replace reelected = 1 if wahlkreis == 183
replace reelected = 1 if wahlkreis == 184
replace reelected = 1 if wahlkreis == 185
replace reelected = 0 if wahlkreis == 186
replace reelected = 1 if wahlkreis == 187
replace reelected = 1 if wahlkreis == 188
replace reelected = 1 if wahlkreis == 189
replace reelected = 1 if wahlkreis == 190
replace reelected = 1 if wahlkreis == 191
replace reelected = 1 if wahlkreis == 192
replace reelected = 1 if wahlkreis == 193
replace reelected = 1 if wahlkreis == 194
replace reelected = 1 if wahlkreis == 195
replace reelected = 1 if wahlkreis == 196
replace reelected = 1 if wahlkreis == 197
replace reelected = 1 if wahlkreis == 198
replace reelected = 1 if wahlkreis == 199
replace reelected = 1 if wahlkreis == 200
replace reelected = 1 if wahlkreis == 201
replace reelected = 1 if wahlkreis == 202
replace reelected = 1 if wahlkreis == 203
replace reelected = 1 if wahlkreis == 204
replace reelected = 1 if wahlkreis == 205
replace reelected = 1 if wahlkreis == 206
replace reelected = 1 if wahlkreis == 207
replace reelected = 1 if wahlkreis == 208
replace reelected = 1 if wahlkreis == 209
replace reelected = 1 if wahlkreis == 210
replace reelected = 1 if wahlkreis == 211
replace reelected = 1 if wahlkreis == 212
replace reelected = 1 if wahlkreis == 213
replace reelected = 1 if wahlkreis == 214
replace reelected = 1 if wahlkreis == 215
replace reelected = 1 if wahlkreis == 216
replace reelected = 1 if wahlkreis == 217
replace reelected = 1 if wahlkreis == 218
replace reelected = 1 if wahlkreis == 219
replace reelected = 1 if wahlkreis == 220
replace reelected = 1 if wahlkreis == 221
replace reelected = 1 if wahlkreis == 222
replace reelected = 1 if wahlkreis == 223
replace reelected = 1 if wahlkreis == 224
replace reelected = 1 if wahlkreis == 225
replace reelected = 1 if wahlkreis == 226
replace reelected = 1 if wahlkreis == 227
replace reelected = 1 if wahlkreis == 228
replace reelected = 1 if wahlkreis == 229
replace reelected = 1 if wahlkreis == 230
replace reelected = 1 if wahlkreis == 231
replace reelected = 1 if wahlkreis == 232
replace reelected = 1 if wahlkreis == 233
replace reelected = 1 if wahlkreis == 234
replace reelected = 1 if wahlkreis == 235
replace reelected = 1 if wahlkreis == 236
replace reelected = 1 if wahlkreis == 237
replace reelected = 1 if wahlkreis == 238
replace reelected = 1 if wahlkreis == 239
replace reelected = 1 if wahlkreis == 240
replace reelected = 1 if wahlkreis == 241
replace reelected = 1 if wahlkreis == 242
replace reelected = 1 if wahlkreis == 243
replace reelected = 1 if wahlkreis == 244
replace reelected = 1 if wahlkreis == 245
replace reelected = 1 if wahlkreis == 246
replace reelected = 1 if wahlkreis == 247
replace reelected = 1 if wahlkreis == 248
replace reelected = 1 if wahlkreis == 249
replace reelected = 1 if wahlkreis == 250
replace reelected = 1 if wahlkreis == 251
replace reelected = 1 if wahlkreis == 252
replace reelected = 1 if wahlkreis == 253
replace reelected = 1 if wahlkreis == 254
replace reelected = 1 if wahlkreis == 255
replace reelected = 1 if wahlkreis == 256
replace reelected = 1 if wahlkreis == 257
replace reelected = 1 if wahlkreis == 258
replace reelected = 1 if wahlkreis == 259
replace reelected = 1 if wahlkreis == 260
replace reelected = 1 if wahlkreis == 261
replace reelected = 1 if wahlkreis == 262
replace reelected = 1 if wahlkreis == 263
replace reelected = 1 if wahlkreis == 264
replace reelected = 1 if wahlkreis == 265
replace reelected = 1 if wahlkreis == 266
replace reelected = 1 if wahlkreis == 267
replace reelected = 1 if wahlkreis == 268
replace reelected = 1 if wahlkreis == 269
replace reelected = 1 if wahlkreis == 270
replace reelected = 1 if wahlkreis == 271
replace reelected = 1 if wahlkreis == 272
replace reelected = 1 if wahlkreis == 273
replace reelected = 1 if wahlkreis == 274
replace reelected = 1 if wahlkreis == 275
replace reelected = 1 if wahlkreis == 276
replace reelected = 1 if wahlkreis == 277
replace reelected = 1 if wahlkreis == 278
replace reelected = 1 if wahlkreis == 279
replace reelected = 1 if wahlkreis == 280
replace reelected = 1 if wahlkreis == 281
replace reelected = 1 if wahlkreis == 282
replace reelected = 1 if wahlkreis == 283
replace reelected = 1 if wahlkreis == 284
replace reelected = 1 if wahlkreis == 285
replace reelected = 1 if wahlkreis == 286
replace reelected = 1 if wahlkreis == 287
replace reelected = 1 if wahlkreis == 288
replace reelected = 1 if wahlkreis == 289
replace reelected = 1 if wahlkreis == 290
replace reelected = 1 if wahlkreis == 291
replace reelected = 1 if wahlkreis == 292
replace reelected = 1 if wahlkreis == 293
replace reelected = 1 if wahlkreis == 294
replace reelected = 1 if wahlkreis == 295
replace reelected = 0 if wahlkreis == 296
replace reelected = 1 if wahlkreis == 297
replace reelected = 1 if wahlkreis == 298
replace reelected = 1 if wahlkreis == 299


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen surveydate = intdate

format %tdMon_DD,_CCYY surveydate

gen edate = 20170924

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = electiondate - surveydate

* Gender

gen male = vn1

recode male (2=0)

* Age

gen age = vn2c

recode age (-99=.)

replace age = 2017 - age

replace age = . if age < 17

* Education

gen education = vn136

recode education (-99=.)
recode education (6=.)
recode education (9=.)

* Interest for election

gen interest = v4

recode interest (-99=.)
recode interest (-98=.)
recode interest (-94=.)

replace interest = 6 - interest

* Caring about outcome

gen care_whole = v5

recode care_whole (-99=.)
recode care_whole (-98=.)
recode care_whole (-94=.)

replace care_whole = 6 - care_whole

* Party identification (national) 

gen pid1 = vn99a

recode pid1 (2=1)
recode pid1 (3=1) // CDU and CSU recoded as CDU/CSU 

gen pid2 = vn99b

recode pid2 (2=1)
recode pid2 (3=1) // CDU and CSU recoded as CDU/CSU 

gen pid = max(pid1,pid2)

gen pidstatus_whole = .

replace pidstatus_whole = 9999 if pid == .
replace pidstatus_whole = 9999 if pid == -98
replace pidstatus_whole = 9999 if pid == -99

replace pidstatus_whole = 2 if pid == 808

replace pidstatus_whole = 3 if pid == 1 & pidstatus_whole!=2 & pidstatus_whole!=9999 | pid == 4 & pidstatus_whole!=2 & pidstatus_whole!=9999

replace pidstatus_whole = 1 if pid!=1 & pid!=4 & vn99a!=. & pidstatus_whole!=2 & pidstatus_whole!=3 & pidstatus_whole!=9999

replace pidstatus_whole = . if pidstatus_whole == 9999

label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID"
label values pidstatus_whole pidstatus

* Party identification scale (national)

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & vn100 == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & vn100 == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & vn100 == 3
replace pidscale_whole = 4 if pidstatus_whole == 1 & vn100 == 4
replace pidscale_whole = 5 if pidstatus_whole == 1 & vn100 == 5
replace pidscale_whole = 6 if pidstatus_whole == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & vn100 == 5
replace pidscale_whole = 8 if pidstatus_whole == 3 & vn100 == 4
replace pidscale_whole = 9 if pidstatus_whole == 3 & vn100 == 3
replace pidscale_whole = 10 if pidstatus_whole == 3 & vn100 == 2
replace pidscale_whole = 11 if pidstatus_whole == 3 & vn100 == 1

label define pidscale 1 "Very strong loser PID" 2 "Fairly strong loser PID" 3 "Moderate loser PID" 4 "Fairly weak loser PID" 5 "Very weak loser PID" 6 "No PID" 7 "Very weak winner PID" 8 "Fairly weak winner PID" 9 "Moderate winner PID" 10 "Fairly strong winner PID" 11 "Very strong winner PID"
label values pidscale_whole pidscale

* Party identification (district)

gen pidstatus_district = .

replace pidstatus_district = 9999 if pid == .
replace pidstatus_district = 9999 if pid == -98
replace pidstatus_district = 9999 if pid == -99

replace pidstatus_district = 2 if pid == 808

replace pidstatus_district = 3 if pid == district_outcome & pidstatus_district!=2 & pidstatus_district!=9999

replace pidstatus_district = 1 if pid!=district_outcome & pid!=. & pidstatus_district!=2 & pidstatus_district!=3 & pidstatus_district!=9999

replace pidstatus_district = . if pidstatus_district == 9999

label values pidstatus_district pidstatus

* Party identification scale (district)

gen pidscale_district = .

replace pidscale_district = 1 if pidstatus_district == 1 & vn100 == 1
replace pidscale_district = 2 if pidstatus_district == 1 & vn100 == 2
replace pidscale_district = 3 if pidstatus_district == 1 & vn100 == 3
replace pidscale_district = 4 if pidstatus_district == 1 & vn100 == 4
replace pidscale_district = 5 if pidstatus_district == 1 & vn100 == 5
replace pidscale_district = 6 if pidstatus_district == 2
replace pidscale_district = 7 if pidstatus_district == 3 & vn100 == 5
replace pidscale_district = 8 if pidstatus_district == 3 & vn100 == 4
replace pidscale_district = 9 if pidstatus_district == 3 & vn100 == 3
replace pidscale_district = 10 if pidstatus_district == 3 & vn100 == 2
replace pidscale_district = 11 if pidstatus_district == 3 & vn100 == 1

label values pidscale_district pidscale

* Coalition preference

gen coalprefA = v73a
gen coalprefB = v73b
gen coalprefC = v73c
gen coalprefD = v73d
gen coalprefE = v73e
gen coalprefF = v73f
gen coalprefG = v73g

recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF coalprefG (-94=.)
recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF coalprefG (-98=.)
recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF coalprefG (-99=.)

gen coalpref_complete = 1 if coalprefA!=. & coalprefB!=. & coalprefC!=. & coalprefD!=. & coalprefE!=. & coalprefF!=. & coalprefG!=.

gen coalpref = 3 if coalprefC >= coalprefA & coalprefC >= coalprefB & coalprefC >= coalprefD & coalprefC >= coalprefE & coalprefC >= coalprefF & coalprefC >= coalprefG & coalpref_complete == 1
replace coalpref = 2 if coalprefA == coalprefB & coalprefA == coalprefC & coalprefA == coalprefD & coalprefA == coalprefE & coalprefA == coalprefF & coalprefA == coalprefG & coalpref_complete == 1
replace coalpref = 1 if coalpref!=2 & coalpref!=3 & coalpref_complete == 1

replace coalpref = . if coalpref == 3 & coalprefC == coalprefA & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefB & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefD & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefE & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefF & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefC == coalprefG & coalpref_complete == 1

label define coalpref 1 "Loser coalition" 2 "No preference" 3 "Winner coalition"
label values coalpref coalpref

* Coalition preference strength

gen coalstrengthA = coalprefA if coalprefA >= coalprefB & coalprefA >= coalprefC & coalprefA >= coalprefD & coalprefA >= coalprefE & coalprefA >= coalprefF & coalprefA >= coalprefG & coalpref_complete == 1
gen coalstrengthB = coalprefB if coalprefB >= coalprefA & coalprefB >= coalprefC & coalprefB >= coalprefD & coalprefB >= coalprefE & coalprefB >= coalprefF & coalprefB >= coalprefG & coalpref_complete == 1
gen coalstrengthC = coalprefC if coalprefC >= coalprefA & coalprefC >= coalprefB & coalprefC >= coalprefD & coalprefC >= coalprefE & coalprefC >= coalprefF & coalprefC >= coalprefG & coalpref_complete == 1
gen coalstrengthD = coalprefD if coalprefD >= coalprefA & coalprefD >= coalprefB & coalprefD >= coalprefC & coalprefD >= coalprefE & coalprefD >= coalprefF & coalprefD >= coalprefG & coalpref_complete == 1
gen coalstrengthE = coalprefE if coalprefE >= coalprefA & coalprefE >= coalprefB & coalprefE >= coalprefC & coalprefE >= coalprefD & coalprefE >= coalprefF & coalprefE >= coalprefG & coalpref_complete == 1
gen coalstrengthF = coalprefF if coalprefF >= coalprefA & coalprefF >= coalprefB & coalprefF >= coalprefC & coalprefF >= coalprefD & coalprefF >= coalprefE & coalprefF >= coalprefG & coalpref_complete == 1
gen coalstrengthG = coalprefG if coalprefG >= coalprefA & coalprefG >= coalprefB & coalprefG >= coalprefC & coalprefG >= coalprefD & coalprefG >= coalprefE & coalprefG >= coalprefF & coalpref_complete == 1

gen coalstrength = max(coalstrengthA,coalstrengthB,coalstrengthC,coalstrengthD,coalstrengthE,coalstrengthF,coalstrengthG)

replace coalstrength = 300 if coalstrength == 8 | coalstrength == 9 | coalstrength == 10 | coalstrength == 11
replace coalstrength = 200 if coalstrength == 5 | coalstrength == 6 | coalstrength == 7
replace coalstrength = 100 if coalstrength == 1 | coalstrength == 2 | coalstrength == 3 | coalstrength == 4

replace coalstrength = 3 if coalstrength == 300
replace coalstrength = 2 if coalstrength == 200
replace coalstrength = 1 if coalstrength == 100

label define coalstrength 1 "Weak preference" 2 "Moderate preference" 3 "Strong preference"
label values coalstrength coalstrength

* Coalition preference scale

gen coalscale = .

replace coalscale = 1 if coalpref == 1 & coalstrength == 3
replace coalscale = 2 if coalpref == 1 & coalstrength == 2
replace coalscale = 3 if coalpref == 1 & coalstrength == 1
replace coalscale = 4 if coalpref == 2
replace coalscale = 5 if coalpref == 3 & coalstrength == 1
replace coalscale = 6 if coalpref == 3 & coalstrength == 2
replace coalscale = 7 if coalpref == 3 & coalstrength == 3

label define coalscale 1 "Strong loser preference" 2 "Moderate loser preference" 3 "Weak loser preference" 4 "No preference" 5 "Weak winner preference" 6 "Moderate winner preference" 7 "Strong winner preference" 
label values coalscale coalscale

* First/second vote and seats in parliament - Correct answer (second vote) = 1, Incorrect answer = 0

gen votes = vn7

recode votes (1=0)
recode votes (2=1)
recode votes (3=0)

recode votes (-98=0)
recode votes (-99=.)

drop if votes == -94

* 5 percent threshold - Correct answer (5 percent) = 1, Incorrect answer = 0

gen threshold = vn14rf

recode threshold (-98=0)
recode threshold (-99=.)

drop if threshold == -94

* Picture of SPD leader - Correct answer (Martin Schulz) = 1, Incorrect answer = 0

gen spdleader = vn127arf

recode spdleader (-98=0)
recode spdleader (-99=.)

drop if spdleader == -94

* Picture of Green leader - Correct answer (Katrin Göring-Eckardt) = 1, Incorrect answer = 0

gen grnleader = vn127brf

recode grnleader (-98=0)
recode grnleader (-99=.)

drop if grnleader == -94

* Picture of FDP leader - Correct answer (Christian Lindner) = 1, Incorrect answer = 0

gen fdpleader = vn127crf

recode fdpleader (-98=0)
recode fdpleader (-99=.)

drop if fdpleader == -94

* Political knowledge index

gen knowledge = votes + threshold + spdleader + grnleader + fdpleader

* Political discussion

gen discussion = v107

recode discussion (-94=.)
recode discussion (-98=.)
recode discussion (-99=.)

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.

* Network size

gen size = v108

recode size (-71=.)
recode size (-94=.)
recode size (-97=.)
recode size (-98=.)
recode size (-99=.)

replace size = 0 if discussion == 0

gen size_3pts = size
replace size_3pts = 0 if size == 1
replace size_3pts = 1 if size == 2
replace size_3pts = 1 if size == 3
replace size_3pts = 1 if size == 4
replace size_3pts = 2 if size > 4 & size!=.

* Network expertise

gen expertise1 = v112
gen expertise2 = v118

recode expertise1 expertise2 (-94=.)
recode expertise1 expertise2 (-97=.)
recode expertise1 expertise2 (-98=.)
recode expertise1 expertise2 (-99=.)

replace expertise1 = 4 - expertise1
replace expertise2 = 4 - expertise2

replace expertise1 = 0 if size == 0
replace expertise2 = 0 if size < 2

gen expertise = expertise1 + expertise2

egen expertise_4pts = rowmax(expertise1 expertise2)

* Political disagreement

gen disagreement1 = v113
gen disagreement2 = v119

recode disagreement1 disagreement2 (-94=.)
recode disagreement1 disagreement2 (-97=.)
recode disagreement1 disagreement2 (-98=.)
recode disagreement1 disagreement2 (-99=.)

replace disagreement1 = 4 - disagreement1
replace disagreement2 = 4 - disagreement2

replace disagreement1 = 0 if size == 0
replace disagreement2 = 0 if size < 2

gen disagreement = disagreement1 + disagreement2

gen disagreement1_3pts = disagreement1
gen disagreement2_3pts = disagreement2

replace disagreement1_3pts = 0 if disagreement1 == 1
replace disagreement1_3pts = 1 if disagreement1 == 2
replace disagreement1_3pts = 2 if disagreement1 == 3

replace disagreement2_3pts = 0 if disagreement2 == 1
replace disagreement2_3pts = 1 if disagreement2 == 2
replace disagreement2_3pts = 2 if disagreement2 == 3

egen disagreement_3pts = rowmax(disagreement1_3pts disagreement2_3pts)

* Main source of information

gen network_main = 1 if v91 == 6

replace network_main = 0 if v91!=6
replace network_main = . if v91 == -98
replace network_main = . if v91 == -99


*********************************
** NATIONAL-LEVEL EXPECTATIONS ** 
*********************************

* Generate variable identifying incomplete answers

gen coalitionA = v75a
gen coalitionB = v75b
gen coalitionC = v75c
gen coalitionD = v75d
gen coalitionE = v75e
gen coalitionF = v75f
gen coalitionG = v75g

recode coalitionA coalitionB coalitionC coalitionD coalitionE coalitionF coalitionG (-94=.) 
recode coalitionA coalitionB coalitionC coalitionD coalitionE coalitionF coalitionG (-98=.) 
recode coalitionA coalitionB coalitionC coalitionD coalitionE coalitionF coalitionG (-99=.) 

* Identify correct and incorrect forecasts

gen correct_whole_d = 1 if coalitionA == 1 & coalitionB == 1 & coalitionC == 0 & coalitionD == 0 & coalitionE == 0 & coalitionF == 0 & coalitionG == 0
replace correct_whole_d = 0 if correct_whole_d!=1
replace correct_whole_d = . if coalitionA == 0 & coalitionB == 0 & coalitionC == 0 & coalitionD == 0 & coalitionE == 0 & coalitionF == 0 & coalitionG == 0
replace correct_whole_d = 0 if v75a == -98 & v75b == -98 & v75c == -98 & v75d == -98 & v75e == -98 & v75f == -98 & v75g == -98


*********************************
** DISTRICT-LEVEL EXPECTATIONS ** 
*********************************

* Forecasts (1 = CDU/CSU, 4 = SPD, 5 = FDP, 6 = Green, 7 = Die Linke, 180 = FW, 322 = AfD, 801 = other)

gen forecast_district1 = v76a
gen forecast_district2 = v76b

recode forecast_district1 forecast_district2 (-94=.) 
recode forecast_district1 forecast_district2 (-99=.) 
recode forecast_district1 forecast_district2 (2=1) 
recode forecast_district1 forecast_district2 (3=1) 

egen forecast_district = rowmax(forecast_district1 forecast_district2)

* Identify correct forecasts

gen correct_district_d = 0 if forecast_district!=district_outcome & forecast_district!=.
replace correct_district_d = 1 if forecast_district == district_outcome & forecast_district!=.


**********
** SAVE **
**********

save "de2017_gles.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion disagreement expertise size coalscale interest care_whole knowledge age i.male education time  
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1

* Regression analysis and percentage of correct and incorrect forecasts (district)

melogit correct_district_d discussion disagreement expertise size pidscale_district interest knowledge age i.male education time enep || wahlkreis: , or
estimates table, star(.05 .01 .001)

tab correct_district_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Coalition formed (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable expertise "Network expertise"
label variable size "Network size"
label variable pidscale_whole "National PID"
label variable pidscale_district "District PID"
label variable coalscale "Coalition preference"
label variable interest "Interest"
label variable care_whole "Caring about outcome"
label variable knowledge "Political knowledge"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"

quietly logistic correct_whole_d discussion disagreement expertise size coalscale interest care_whole knowledge age i.male education time

estpost tabstat correct_whole_d discussion disagreement expertise size coalscale interest care_whole knowledge age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "de2017_gles_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2017 German federal election (GLES), national level") replace

eststo clear

quietly melogit correct_district_d discussion disagreement expertise size pidscale_district interest knowledge age i.male education time enep i.reelected || wahlkreis: , or

estpost tabstat correct_district_d discussion disagreement expertise size pidscale_district interest knowledge age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "de2017_gles_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2017 German federal election (GLES), district level") replace

eststo clear


*************************
** ADDITIONAL ANALYSES **
*************************

* Regression analysis (national)

logistic correct_whole_d i.network_main discussion disagreement expertise size coalscale interest care_whole knowledge age i.male education time  
estimates table, star(.05 .01 .001)

* Regression analysis (district)

melogit correct_district_d i.network_main discussion disagreement expertise size pidscale_district interest knowledge age i.male education time enep i.reelected || wahlkreis: , or
estimates table, star(.05 .01 .001)

log close